package final_project;

import java.util.*;

public class Patent {
    private String applicationNo;
    private Date applicationDate;
    private LinkedHashSet<Ipc> ipcs;
    private LinkedHashSet<Cpc> cpcs;

    private final static String SEPERATOR = ",";

    public Patent(Date applicationDate, LinkedHashSet<Ipc> ipcs, LinkedHashSet<Cpc> cpcs) {
        this.applicationDate = applicationDate;
        this.ipcs = ipcs;
        this.cpcs = cpcs;
    }

    public Patent(Date applicationDate, Ipc[] ipcs, Cpc[] cpcs) {
        this.applicationDate = applicationDate;
        this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs));
        this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs));
    }

    public Patent(String applicationDate, String multiIpcs, String multiCpcs, char position, char value) {
        this.applicationDate = new Date(applicationDate);

        String[] ipcSymbols = multiIpcs.split(SEPERATOR);
        this.ipcs = new LinkedHashSet<Ipc>(ipcSymbols.length);
        //ipcs 是刚初始化的空集合，cpcs.size() 为 0，循环不会执行，导致 ipcs 始终为空。
        for (int i = 0; i < ipcSymbols.length; i++) {
            Ipc ipc = new Ipc(ipcSymbols[i].trim());
            ipcs.add(ipc);
        }

        String[] cpcSymbols = multiCpcs.split(SEPERATOR);
        this.cpcs = new LinkedHashSet<Cpc>(cpcSymbols.length);
        for (int i = 0; i < cpcSymbols.length; i++) {
            Cpc cpc = new Cpc(cpcSymbols[i].trim(), position, value);
            cpcs.add(cpc);
        }
    }

    public Date getApplicationDate() {
        return applicationDate;
    }

    public void setApplicationDate(Date applicationDate) {
        this.applicationDate = applicationDate;
    }

    public HashSet<Ipc> getIpcs() {
        return ipcs;
    }

    public void setIpcs(Ipc[] ipcs) {
        this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs));
    }

    public HashSet<Cpc> getCpcs() {
        return cpcs;
    }

    public void setCpcs(Cpc[] cpcs) {
        this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs));
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append(applicationDate + "\t");

        sb.append("[");
        List<Ipc> listIpcs = new Vector<Ipc>(ipcs);
        for (int i = 0; i < listIpcs.size() - 1; i++) {
            sb.append(listIpcs.get(i) + SEPERATOR);
        }
        sb.append(listIpcs.get(listIpcs.size() - 1) + "]\t");

        sb.append("[");
        List<Cpc> listCpcs = new Vector<Cpc>(cpcs);
        for (int i = 0; i < listCpcs.size() - 1; i++) {
            sb.append(listCpcs.get(i) + SEPERATOR);
        }
        sb.append(listCpcs.get(listCpcs.size() - 1) + "]");

        return sb.toString();
    }
}